package com.canbot.u05.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.canbot.u05.MainApplication;
import com.canbot.u05.R;
import com.canbot.u05.bean.CustomDialogue;
import com.canbot.u05.utils.ConstUtils;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.ToastTools;

import java.util.ArrayList;
import java.util.regex.Pattern;

public class CVVDialogEditActivity extends BaseActivityNoTitle {

        private RelativeLayout mLayoutHead;

        private Button mBack;

        private TextView mTextView;

        private Button bt_Sure;

        private EditText editText;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.cvvd_activity2);
                initView();
        }

        private void initView() {
                mLayoutHead = (RelativeLayout) findViewById(R.id.head_language);
                mBack = (Button) mLayoutHead.findViewById(R.id.btn_head_back);
                mTextView = (TextView) mLayoutHead.findViewById(R.id.tv_head);
                mTextView.setText("自定义声控对话");
                bt_Sure = (Button) findViewById(R.id.btn_sure);
                editText = (EditText) findViewById(R.id.et_username);
                editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(15)});
                editText.addTextChangedListener(new TextWatcher() {

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before,
                                                  int count) {
                                if (count > 0) {
                                        bt_Sure.setBackgroundResource(R.drawable.icon_dialogyes_00);
                                }
                                else if (count == 0) {
                                        bt_Sure.setBackgroundResource(R.drawable.icon_dialogyes_01);
                                }

                        }

                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count,
                                                      int after) {


                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                                // TODO Auto-generated method stub

                        }
                });

                mBack.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                                finish();

                        }
                });
                bt_Sure.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                                if (editText.getText().toString().length() > 0) {

                                        Logger.e("save", "question" + editText.getText().toString());
                                        if(!check(editText.getText().toString())){
                                                ToastTools.newInstance(CVVDialogEditActivity.this).showText(getString(R.string.problem_contains_illegal_characters));
                                                return;
                                        }

                                        ArrayList<CustomDialogue> customDialoguestList=((MainApplication) getApplication()).mCustomDialogues;
                                        for(int i=0;i<customDialoguestList.size();i++){
                                               if(customDialoguestList.get(i).getQuestion().equals(editText.getText().toString())){
                                                       ToastTools.newInstance(CVVDialogEditActivity.this).showText(getString(R.string.question_cannot_repeated));
                                                       return;
                                               }
                                        }

                                                Intent mIntent = new Intent(CVVDialogEditActivity.this, RecordActivity.class);
                                                mIntent.putExtra("question", "" + editText.getText().toString());
                                                mIntent.putExtra("fromActivity", ConstUtils.ACTIVITY_VCC);
                                                startActivity(mIntent);
                                                CVVDialogEditActivity.this.finish();


                                }
                                else {
                                        Log.i("", "不能为空");
                                }

                        }
                });

        }
        /**
         * 验证只包含字母，数字，中文
         * @param str
         * @return
         */
        public static boolean check(String str){
                String all = "^[a-zA-Z0-9\\u4e00-\\u9fa5]+$";
                Pattern pattern = Pattern.compile(all);
                return pattern.matches(all,str);
        }

}
